<template>
    <div class="app-container">
        <el-card shadow="never">
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
                <el-tab-pane label="采购申请" :name="1">
                    <!-- 条件查询 -->
                    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                        <el-row>
                            <el-col :md="6" :sm="12">
                                <el-form-item label="编号:" prop="brand.value">
                                    <el-input v-model="queryParams.brand.value" placeholder="请输入编号" clearable />
                                </el-form-item>
                            </el-col>

                            <el-col :md="6" :sm="12">
                                <el-form-item class="ele-text-right" label-width="50px">
                                    <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                                    <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>


                    <qa-table
                        :source="tableData"
                        :columns="columns"
                        :selection="true"
                        :loading="loading"
                        @sortChange="sortChange"
                        @statusChange="statusChange"
                        @selectionChange="selectionChange"
                        id="quickadmin"
                    >
                        <template v-slot:action>
                            <el-table-column label="操作" width="240" align="left">
                                <template slot-scope="scope">

                                    <!-- <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                        <router-link
                                                :to="{
                                                    path: 'output_detail',
                                                    query: {
                                                        id: scope.row.id,
                                                    }
                                                }"
                                            >
                                                采购申请列表
                                        </router-link>
                                    </el-button> -->
                                    <el-button class="btn-mar" size="mini" type="primary" @click="handleEditsku(scope.row)" v-hasPermi="api.editsku">编辑</el-button>
                                    <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit2">添加采购申请</el-button>
                                    <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEditsku(scope.row)" v-hasPermi="api.editsku">采购成本</el-button> -->
                                    <!-- <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button> -->
                                    <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                                    <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->

                                </template>
                            </el-table-column>
                        </template>
                    </qa-table>

                <!-- 分页 -->
                    <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                </el-tab-pane>
                <el-tab-pane label="手动申请" :name="2"><!-- 条件查询 -->
                    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                        <el-row>
                            <el-col :md="6" :sm="12">
                                <el-form-item label="编号:" prop="brand.value">
                                    <el-input v-model="queryParams.brand.value" placeholder="请输入编号" clearable />
                                </el-form-item>
                            </el-col>

                            <el-col :md="6" :sm="12">
                                <el-form-item class="ele-text-right" label-width="50px">
                                    <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                                    <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>


                    <qa-table
                        :source="tableData"
                        :columns="columns"
                        :selection="true"
                        :loading="loading"
                        @sortChange="sortChange"
                        @statusChange="statusChange"
                        @selectionChange="selectionChange"
                        id="quickadmin"
                    >
                        <template v-slot:action>
                            <el-table-column label="操作" width="240" align="left">
                                <template slot-scope="scope">

                                    <!-- <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                        <router-link
                                                :to="{
                                                    path: 'output_detail',
                                                    query: {
                                                        id: scope.row.id,
                                                    }
                                                }"
                                            >
                                                采购申请列表
                                        </router-link>
                                    </el-button> -->
                                    <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-plus" @click="handleEdit(scope.row)" v-hasPermi="api.edit">添加采购申请</el-button>
                                    <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-plus" @click="handleEditsku(scope.row)" v-hasPermi="api.editsku">采购成本</el-button>
                                    <!-- <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button> -->
                                    <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                                    <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->

                                </template>
                            </el-table-column>
                        </template>
                    </qa-table>

                    <!-- 分页 -->
                    <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                </el-tab-pane>
               
            </el-tabs>
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editpurchaseOpen"  :before-close="beforeClosepurchase" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitFormpurchase="submitFormpurchase" :editFormParams="editFormParams" @editFormCancel="beforeClosepurchase" />
        </el-dialog>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditSKUForm ref="EditSKUForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormDetailCom'
import EditSKUForm from './EditSKUFormDetailCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm ,EditSKUForm},
    
    data() {
        return {
            activeName:1,
            // table结构
            columns: [
                // {
                //     visible: true,
                //     label: 'ID',
                //     prop: 'id',
                // },
                {
                    visible: true,
                    label: '编号',
                    prop: 'brand',
                },
                {
                    visible: true,
                    label: '产品图片',
                    prop: 'images',
                    component:'QuickAdminImage',
                },

                {
                    visible: true,
                    label: '到货天数',
                    prop: 'arrive_day',
                },
                // {
                //     visible: true,
                //     label: '单位',
                //     prop: 'unit',
                // },
                {
                    visible: true,
                    label: '采购成本（显示最新月份）',
                    prop: 'purchase_cost',
                },
                {
                    visible: true,
                    label: '库存',
                    prop: 'inventory',
                },
         
            ],
            // 接口地址
            api: {
                index: `/admin/output/index`,
                find: `/admin/output/find`,
                addpurchase: `/admin/output/add_purchase_apply`,
                editpurchase: `/admin/output/add_purchase_apply3`,
                add: `/admin/output/edit_purchaseprice`,
                edit: `/admin/output/update_arrive_day`,
                edit2: `/admin/output/add_purchase_apply`,
                editsku: `/admin/output/add_purchaseprice`,
                update_purchase_cost: `/admin/output/update_purchase_cost`,
                update_arrive_day: `/admin/output/update_arrive_day`,
                delete: `/admin/output/delete`,
                export: `/admin/output/export`,
                status: `/admin/output/status`,

            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                brand: { value:undefined, op: '%like%'},

            },
            editpurchaseOpen: false,
        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        handleClick(tab, event) {
            console.log(tab, event);
        },
        async blurs(scope) {
            this.$refs.mytb.setCurrentRow();
            let response = await this.request.post(this.api.update_purchase_cost, { 
                product_id: scope.row.id,
                purchase_cost: scope.row.purchase_cost
            })
            this.initIndex()
        },
        // 编辑
        handleEdit(row) {
            this.handleFind(row[this.primaryKey])
        },

        // 编辑回显
        async handleFind(product_id) {
            let response = await this.request.post(this.api.edit2, { 
                product_id: product_id  
            })
            this.findFormData = this.formatFindData(response)
        
            this.editpurchaseOpen = true
        },
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },

        // 编辑
        handleEditsku(row) {
            this.handleFindsku(row[this.primaryKey])
        },

        // 编辑回显
        async handleFindsku(id) {
            let response = await this.request.get(this.api.update_arrive_day, { params: { id: id } })
            this.findFormData = this.formatFindData(response)
            this.editOpen = true
        },
        
    }
}
</script>
<style lang="scss" scoped>
.btn-mar {
    margin: 5px !important;
}
.el-table {
    // 默认情况下，即没有选中某一行时，只显示span
    span {
        display: block;
    }
    .el-input {
        display: none;
    }
    .el-button2{
        display: none;
    }
    // .el-select {
    //     display: none;
    // }
  // 当点击某一行时，对应的行会添加current-row类，则内部的新的样式会生效
    .current-row {
        span {
        display: none;
        }
        .el-input {
            display: block;
        }
        .el-button2{
            display: block;
        }
        .el-select {
            display: block;
        }
    }
}
</style>
